(HOST) ->
    waitFor = (conditionFunction) ->

      poll = (resolve) ->
        if conditionFunction()
          resolve()
        else
          setTimeout (->
            poll resolve
            return
          ), 400
        return

      new Promise(poll)


    THEME_NAME = "单间 共享办公 日租 工位 会议室".split(" ")

    do ->
        await waitFor(
            =>
                if $("#logo")[0]
                    if not begin
                        begin = new Date()
                    if (new Date()) - begin > 5000
                        location.reload()
                if not REACT.store
                    return false
                store = REACT.store.getState()
                if not store.fmapThemeCtrl?.fmapThemeAPI
                    return false
                mapData = store.fmapDataCtrl?.fmapDataStates?.fmapDataAPI?.mapData
                if mapData
                    for k,v of mapData
                        return true
                return false
        )
        store = REACT.store.getState()

        id2name = {}

        theme = store.fmapThemeCtrl.fmapThemeAPI.mapTheme
        for id,o of theme.storeThemes
            id2name[id] = o.name

        theme_name = {}
        theme_type = new Set()

        for id, o of theme.storethemeTypeApply
            name = id2name[o.normalid]
            if THEME_NAME.indexOf(name) + 1
                theme_name[id]=name
                theme_type.add(id)

        map = store.fmapDataCtrl.fmapDataStates.fmapDataAPI

        floor_li = {}
        for floor,floor_map of map.mapData
            floor_li[floor] = li = []
            for i in floor_map.store.features
                properties = i.properties
                type = properties.type
                if theme_type.has(type)
                    li.push [
                        properties.sid-0
                        properties.name
                        type-0
                        # i.geometry.coordinates[0]
                    ]

        data = JSON.stringify([store.fmapDataCtrl.curMapID, theme_name, floor_li])
        GM_xmlhttpRequest {
            contentType: "application/json"
            data
            method: 'POST'
            url: "#{HOST.API}/ur/build/fengmap/set"
            onload:(responseDetails)=>
                id = responseDetails.responseText

                $.sweetModal(
                    {
                        content:"""<h1>地图导入成功<br><a target="_blank" href="#{HOST.SITE}/build/fengmap/#{id}">点此绑定大楼</a></h1>"""
                        icon: $.sweetModal.ICON_SUCCESS
                    }
                )
        }


#    {data} = (await $.getJSON("/fengmap-editor/themefile/"+REACT.store.getState().+"?accessToken="+unsafeWindow.token))
#    data = JSON.parse(LZString.decompress(data))
#    console.log data
#    for i in data.storetheme
#        console.log i.name, i



###
API_URL = undefined

ROUTE = {
    subscribers:->
        ing = undefined
        id = "mail-subscribers"
        if $("##{id}")[0]
            return
        btn = $ """<a class="gh-btn gh-btn-blue ember-view" id="#{id}" href="#"><span>群发邮件</span></a>"""
        $(".view-actions").prepend btn
        btn.click ->
            if ing
                return
            ing = 1
            $.getJSON("#{API_URL}configuration/").then ({configuration})->

                ghost.init configuration[0]
                post_li = await $.getJSON(ghost.url.api('posts', {limit: 1, fields:"title,url,custom_excerpt"}))
                li = []
                for i in post_li.posts
                    i.url = location.protocol+"//"+location.host+i.url
                    box = $ '<div><input><textarea></textarea><button>群发订阅提醒邮件</button></div>'
                    input = box.find('input').val(document.title.split("-").pop().trim() + " 更新提醒 : "+i.title)
                    textarea = box.find('textarea').val("《"+i.title+"》\n\n"+i.custom_excerpt+"\n\n"+"全文链接 : "+i.url)
                    fancybox = $.fancybox.open(box,{
                        smallBtn:false
                        clickOutside:false
                        clickSlide:false
                        touch:false
                        buttons:['close']
                        afterClose:->
                            ing = undefined
                        afterShow:->
                            box.find('button').click =>
                                title = input.val()
                                txt = textarea.val()
                                @$slide.parents('.fancybox-inner').find('.fancybox-button--close').remove()
                                @$content.html(
                                    """<div style="text-align:center;font-size:18px"><img src="//qiniu.xvc.com/spin.svg" style="vertical-align:-13px;"> 「#{_.escape title}」发送中</div>"""
                                )
                                $.post(
                                    "https://api.xvc.men/mail/send"
                                    {
                                        url:"#{API_URL}subscribers/csv?access_token=#{JSON.parse(localStorage.getItem("ghost:session")).authenticated.access_token}"
                                        title
                                        txt
                                    }
                                    =>
                                        sended = $ """<div style="text-align:center"><h1 style="font-size:21px"><p>「#{_.escape title}」</p><p style="color:#519703">发送成功</p> </h1><button>干得不错 ！</button></div>"""
                                        @$content.html sended
                                        sended.find('button').click -> fancybox.close()
                                    "json"
                                )

                                false

                    })

                    break
            false

}

route = ->
    setTimeout(
        ->
            action = location.hash.slice(2).split("?")[0]
            ROUTE[action]?()
        300
    )

$.getScript("/public/ghost-sdk.min.js").then ->
    API_URL = ghost.url.api()
    $ route
    $(window).on 'hashchange', route
###
